# GScore 仓库镜像同步工具

param(
    [string]$Mirror
)

# 优先使用参数，其次环境变量，最后默认值
if ([string]::IsNullOrEmpty($Mirror)) {
    if (-not [string]::IsNullOrEmpty($env:MIRROR)) {
        $Mirror = $env:MIRROR
    } else {
        $Mirror = "gitcode"
    }
}

# 默认插件目录
$PluginsDir = "gsuid_core\plugins"

function Write-ColorText {
    param([string]$Text, [string]$Color = "White")
    switch ($Color) {
        "Red" { Write-Host $Text -ForegroundColor Red }
        "Green" { Write-Host $Text -ForegroundColor Green }
        "Yellow" { Write-Host $Text -ForegroundColor Yellow }
        default { Write-Host $Text }
    }
}

# 验证镜像源
if ($Mirror -ne "gitcode" -and $Mirror -ne "cnb") {
    Write-ColorText "错误: 镜像源必须是 'gitcode' 或 'cnb'" "Red"
    exit 1
}

# 根据镜像源设置目标 URL
if ($Mirror -eq "gitcode") {
    $CoreUrl = "https://gitcode.com/gscore-mirror/gsuid_core"
    $PluginUrlPrefix = "https://gitcode.com/gscore-mirror/"
    $MirrorName = "GitCode"
} else {
    $CoreUrl = "https://cnb.cool/gscore-mirror/gsuid_core"
    $PluginUrlPrefix = "https://cnb.cool/gscore-mirror/"
    $MirrorName = "CNB"
}

Write-ColorText "开始替换 git remote url..." "Green"
Write-Host "镜像源: $MirrorName ($Mirror)"
Write-Host "插件目录: $PluginsDir`n"

$Plugins = @(
    "XutheringWavesUID", "RoverSign", "DNAUID", "GenshinUID", "StarRailUID",
    "ZZZeroUID", "DeltaUID", "SayuStock", "BlueArchiveUID", "VAUID",
    "MajsoulUID", "WzryUID", "LOLegendsUID", "CS2UID", "ScoreEcho", "TodayEcho"
)

$Total = 0
$Success = 0
$Skipped = 0

# 处理 gsuid_core 本体
if (Test-Path ".git") {
    $currentUrl = git remote get-url origin 2>$null
    if ([string]::IsNullOrEmpty($currentUrl)) {
        Write-ColorText "  ⊘ 无 origin remote" "Yellow"
        $Skipped++
    } else {
        Write-Host "  当前: $currentUrl"
        Write-Host "  目标: $CoreUrl"

        # 检查是否已是目标镜像地址
        $isTargetMirror = $false
        if ($currentUrl -like "*gitcode.com/gscore-mirror*" -and $Mirror -eq "gitcode") {
            $isTargetMirror = $true
        } elseif ($currentUrl -like "*cnb.cool/gscore-mirror*" -and $Mirror -eq "cnb") {
            $isTargetMirror = $true
        }

        if ($isTargetMirror) {
            Write-ColorText "  ✓ 已是目标镜像地址" "Green"
            $Success++
        } else {
            git remote set-url origin $CoreUrl 2>$null
            Write-ColorText "  ✓ 成功" "Green"
            $Success++
        }
    }
} else {
    Write-ColorText "  ⊘ 非 git 仓库或目录不存在" "Yellow"
    $Skipped++
}
Write-Host ""

# 检查并收集已存在的插件
$ExistingPlugins = @()
foreach ($plugin in $Plugins) {
    $pluginPath = Join-Path $PluginsDir $plugin
    if (Test-Path $pluginPath) {
        $ExistingPlugins += $plugin
    }
}

if ($ExistingPlugins.Count -eq 0) {
    Write-ColorText "未找到任何插件目录，请检查路径: $PluginsDir" "Yellow"
    exit 0
}

Write-Host "发现 $($ExistingPlugins.Count) 个插件目录，开始处理...`n"

# 处理插件
foreach ($plugin in $ExistingPlugins) {
    $pluginPath = Join-Path $PluginsDir $plugin

    Write-Host "处理插件: $plugin"

    if (-not (Test-Path (Join-Path $pluginPath ".git"))) {
        Write-ColorText "  ⊘ 非 git 仓库" "Yellow"
        $Skipped++
        Write-Host ""
        continue
    }

    Push-Location $pluginPath -ErrorAction SilentlyContinue
    $currentUrl = git remote get-url origin 2>$null

    if ([string]::IsNullOrEmpty($currentUrl)) {
        Write-ColorText "  ⊘ 无 origin remote" "Yellow"
        $Skipped++
        Pop-Location
        Write-Host ""
        continue
    }

    $targetUrl = $PluginUrlPrefix + $plugin
    Write-Host "  当前: $currentUrl"
    Write-Host "  目标: $targetUrl"

    # 检查是否已是目标镜像地址
    $isTargetMirror = $false
    if ($currentUrl -like "*gitcode.com/gscore-mirror*" -and $Mirror -eq "gitcode") {
        $isTargetMirror = $true
    } elseif ($currentUrl -like "*cnb.cool/gscore-mirror*" -and $Mirror -eq "cnb") {
        $isTargetMirror = $true
    }

    if ($isTargetMirror) {
        Write-ColorText "  ✓ 已是目标镜像地址" "Green"
        $Success++
        Pop-Location
        Write-Host ""
        continue
    }

    git remote set-url origin $targetUrl 2>$null
    Write-ColorText "  ✓ 成功" "Green"
    $Success++
    Pop-Location
    Write-Host ""
}

$Total = $ExistingPlugins.Count + 1
Write-Host "========================================"
Write-Host "完成！总计: $Total | 成功: $Success | 跳过: $Skipped"
Write-Host "========================================"
